<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 
    定义 : 扁平化就是将多维数组变成一维数组,不存在数组的嵌套
    可以指定多维数组展开的层数，默认为1，就是展开二维数组，以此类推，Infinity就是展开所有
   -->
    <script>
      function test(arr, depth) {
        if(depth === 0) return arr
        return arr.reduce((pre, cur) => pre.concat(Array.isArray(cur) ? test(cur, depth - 1) : cur), [])
      }
      const arr = [1, [2, [3, [4, 5]]], 6,[2,3,4,[1,2,3]]];
      let a = arr.flat(1);
      console.log(a);
      function flatten(arr,deepth = 1) {
        if(deepth <= 0) return arr
        return arr.reduce((pre, cur) => {
          return (pre = pre.concat(Array.isArray(cur) ? flatten(cur,deepth-1) : cur));
        }, []);
      }
      let b = flatten(arr,1)
      console.log(b);
    </script>
  </body>
</html>
